package weaver.interfaces.zhlx.duss.action.hrmresourcemodule.hcm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.HcmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 劳动合同续签申请流程调用SAP劳动合同续签接口
 * @ModifyLog:
 **/
public class WeaLdhtxqsqlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaLdhtxqsqlcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用HCM系统接口返回:";
            RecordSet rs = new RecordSet();
            HcmUtil hcmUtil = new HcmUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String xqrgh = Util.null2String(rs.getString("xqrgh"));
            String xhtksrq1 = hcmUtil.formatDate(Util.null2String(rs.getString("xhtksrq1")));
            String htbh = Util.null2String(rs.getString("htbh"));
            String xhtlx1 = Util.null2String(rs.getString("xhtlx1"));
            String xhtzzrq1 = hcmUtil.formatDate(Util.null2String(rs.getString("xhtzzrq1")));
            String xhtqxfl1 = Util.null2String(rs.getString("xhtqxfl1"));
            String htqxn1 = Util.null2String(rs.getString("htqxn1"));
            String htgwlx1 = Util.null2String(rs.getString("htgwlx1"));
            String xhtzt1 = Util.null2String(rs.getString("xhtzt1"));
            String htqdzt = Util.null2String(rs.getString("htqdzt"));
            JSONArray dataList = new JSONArray();
            JSONObject dataJson = new JSONObject();
            dataJson.put("PERNR",xqrgh);
            dataJson.put("BEGDA",xhtksrq1);
            dataJson.put("ENDDA","99991231");
            dataJson.put("CTNUM",htbh);
            dataJson.put("CTTYP",xhtlx1);
            dataJson.put("ZZCTEDT",xhtzzrq1);
            dataJson.put("ZZHTQXFL",xhtqxfl1);
            dataJson.put("ZZHTQX",htqxn1);
            dataJson.put("ZZSYQX","");
            dataJson.put("ZHR_SYQDQR","");
            dataJson.put("ZZHTGWLX",htgwlx1);
            dataJson.put("ZZHTZT",xhtzt1);
            dataJson.put("ZZJFDW",htqdzt);
            JSONArray P0019List = new JSONArray();
            JSONObject P0019Json = new JSONObject();
            P0019Json.put("PERNR",xqrgh);
            P0019Json.put("BEGDA",xhtksrq1);
            P0019Json.put("TMART","Z1");
            P0019Json.put("TERMN",xhtzzrq1);
            P0019List.add(P0019Json);
            dataJson.put("P0019",P0019List);
            dataList.add(dataJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaLdhtxqsqlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaLdhtxqsqlcAction>>>>>dataList=" + dataList);
            JSONObject resultJson = hcmUtil.handleOKHttpRequest("EC202", dataList, dataMap);
            writeLog(">>>>>WeaLdhtxqsqlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if (!"S".equals(msgType)) {
                    message += outputJson.getString("MSG_TEXT");
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaLdhtxqsqlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}